package com.ilink.teacherservice.service.impl.xiaolian;

import com.aliyun.bailian20231229.models.ListIndicesResponse;
import com.aliyun.bailian20231229.models.RetrieveResponse;
import com.ilink.ilinkcommon.config.BailianConfigNew;
import com.ilink.ilinkcommon.config.XiaoLianPromptConfig;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.teacherservice.entity.DTO.xiaolian.ReferenceDetail;
import com.ilink.teacherservice.entity.DTO.xiaolian.XiaolianSingleChatDTO;
import com.ilink.teacherservice.service.bailian.ListIndicesService;
import com.ilink.teacherservice.service.impl.bailian.RetrieveServiceImpl;
import com.ilink.teacherservice.service.xiaolian.XiaolianIndexService;
import com.ilink.teacherservice.utils.bailian.BailianFileTools;
import com.ilink.teacherservice.utils.bailian.BailianModelTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@Transactional
public class XiaolianIndexServiceImpl implements XiaolianIndexService {

    @Autowired
    ListIndicesService listIndicesService;

    @Autowired
    RetrieveServiceImpl retrieveService;

    @Override
    public R normalChat(String query) throws Exception {
        String prompt = "你是一个答疑老师，回答用户问题，回答形式为一段话，不要使用markdown的格式输出";
        String userInput = BailianModelTools.buildUserInput(query);
        String LLMOutput = BailianModelTools.runLLM(prompt, userInput);
        // Thread.sleep(60000);
        return R.ok(LLMOutput);
    }

    @Override
    public R testRetrieve(String query) throws Exception {
        com.aliyun.bailian20231229.Client client = BailianConfigNew.createBailianClient();
        // 知识库索引名称
        String className = "ilink_project";
        ListIndicesResponse listIndicesResponse = listIndicesService.listIndices(client, className, BailianConfigNew.workspaceId);

        if (!BailianFileTools.checkResponse(listIndicesResponse)) {
            return R.error("获取索引列表失败", listIndicesResponse.getBody().getMessage());
        }
        // 获取对应知识库索引ID
        String indexId = BailianFileTools.getIndexIdByName(listIndicesResponse, className);

        // 在指定知识库索引检索
        RetrieveResponse retrieve = retrieveService.retrieve(client, query, indexId, BailianConfigNew.workspaceId);
        if (!BailianFileTools.checkResponse(retrieve)) {
            return R.error("检索接口调用失败", retrieve.getBody().getMessage());
        }
        return R.ok(retrieve);
    }

    // 返回一个固定的回复模板
    @Override
    public R agentDemo(String query) throws Exception {
        XiaolianSingleChatDTO response = new XiaolianSingleChatDTO();
        String prompt = XiaoLianPromptConfig.PROMPT0;
        response.setContent(prompt);
        response.setSender("AI");
        response.setRecommendedQuestions(Arrays.asList(
            "什么是大模型？",
            "什么智能体Agent？",
            "怎么开始入门智能体应用开发？"
        ));

        // 构造 referenceDetails
        List<ReferenceDetail> referenceDetails = new ArrayList<>();
        referenceDetails.add(new ReferenceDetail("/user/learningNew/course", "学习广场-课程"));
        referenceDetails.add(new ReferenceDetail("/user/learningNew/skill-tree", "学习广场-技能树"));
        referenceDetails.add(new ReferenceDetail("/user/practice", "实训广场"));
        referenceDetails.add(new ReferenceDetail("/user/model", "模型广场"));
        referenceDetails.add(new ReferenceDetail("/user/data", "数据广场"));
        response.setReferenceDetails(referenceDetails);
        return R.ok(response);
    }
}
